package namespace

import (
	"context"
	"gitee/yinlai/kms-backend/config"
	"gitee/yinlai/kms-backend/controllers"
	"gitee/yinlai/kms-backend/utils/logs"
	"github.com/gin-gonic/gin"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"net/http"
)

func Update(r *gin.Context) {
	logs.Debug(nil, "更新namespace")
	var ns v1.Namespace
	clientset, basicInfo, err := controllers.BasicInit(r, &ns)
	returnData := config.ReturnData{}
	if err != nil {
		returnData.Message = err.Error()
		returnData.Status = 400
		r.JSON(http.StatusOK, returnData)
		return
	}

	_, err = clientset.CoreV1().Namespaces().Update(context.TODO(), &ns, metav1.UpdateOptions{})
	if err != nil {
		logs.Error(map[string]interface{}{"id": basicInfo.ClusterId, "msg": err.Error()}, "更新Namespace失败")
		msg := "更新Namespace失败：" + err.Error()
		returnData.Status = 400
		returnData.Message = msg
	} else {
		returnData.Message = "更新成功"
		returnData.Status = 200
	}
	r.JSON(http.StatusOK, returnData)
}
